

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    parentid:'',
    restaurantId: '',
    isScheduled: '',
    scheduledTime: '',
    orderType: '',
    currentTab: 0,
    hotFoosList:[],
    imgUrl:applet_park_api_url_imgUrl,
    navitems:[
    //   {
    //     src:applet_park_api_url_imgUrl+'/ct-3.png',
    //     name:'付款码'
    //  },
      // {
      //   src: applet_park_api_url_imgUrl+'/ct-2.png',
      //   name: '优惠券'
      // },
      {
        src: applet_park_api_url_imgUrl+ '/ct-1.png',
        name: '我的订单'
      },
      // {
      //   src: applet_park_api_url_imgUrl + '/ct-4.png',
      //   name: '会员积分'
      // },
    ],
    navs: [],
  },
  swichNav: function (e) {
    let that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (e.target.dataset.current == 2) {
      this.component_msg.loadData();

    }
    this.loadunreadNumber();
  },
  onLoad: function (options) {
    if (options.restaurantId) {
      this.data.restaurantId = options.restaurantId;
    }
    if (options.orderType) {
      this.data.orderType = options.orderType;
    }
    if (options.scheduledTime) {
      this.data.scheduledTime = options.scheduledTime;
    }
    if (options.isScheduled) {
      this.data.isScheduled = options.isScheduled;
    }
    this.setData(this.data);
    this.loadData();
    this.getBanner();
  },
  getBanner(){//获取banner图 和 热门推荐图片
     var that = this;
     wx.request({
     url: applet_park_api_url + '/api/resImages?restaurantId='+this.data.restaurantId,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        
        if (res.data.code == 0) {
           var navsImg = res.data.data.restaurant.images.split(',');
           var hotFoosList = res.data.data.dishTypes;
           
           var newNavsImg = [];
           var newHotFoosList = [];
           
           for(var i=0;i<navsImg.length;i++){
               if(navsImg[i] != ""){
                var item =applet_park_api_url_imgUrl +'/'+ navsImg[i];
                newNavsImg = newNavsImg.concat(item);
               }
           }

           for(var i=0;i<hotFoosList.length;i++){
             var item =applet_park_api_url_imgUrl +'/'+hotFoosList[i].image;
             var itemId = hotFoosList[i].id;
             var itemParentid = hotFoosList[i].parentid;
             var itemFoodid = hotFoosList[i].id;
             var itemPackingCharges = hotFoosList[i].packingCharges;
             var itemOpeningTime = hotFoosList[i].openingTime;
             var itemEndTime = hotFoosList[i].endTime;
             item=item.split(',')[0];
             var obj = {
                 image: item,
                 id: itemId,
                 parentid: itemParentid,
                 foodid: itemFoodid,
                 packingCharges:itemPackingCharges,
                 openingTime: itemOpeningTime,
                 endTime: itemEndTime
             }
             newHotFoosList = newHotFoosList.concat(obj)
           }

           that.setData({
               navs:newNavsImg,  //轮播图
               hotFoosList:newHotFoosList //热销图
           })
           
        } else {
         
        }
      },
      fail: function (res) {
        
      }
    })
  },
  
  enterdishesDetail(e){//跳转菜品详情页面  直接付款
    
    var PAGE = e.currentTarget.dataset.page;
    var id = e.currentTarget.dataset.id;
    var parentid = e.currentTarget.dataset.parentid;
    var foodid = e.currentTarget.dataset.foodid;
    var packingCharges = e.currentTarget.dataset.packingcharges;
    var openingTime = e.currentTarget.dataset.openingtime;
    var endTime = e.currentTarget.dataset.endtime;
    
    
    
    if(openingTime && endTime){
      if(this.time_range (openingTime, endTime)){
        wx.navigateTo({
          url: '../dishesDetail/dishesDetail?pages='+PAGE+'&dishsid='+id+'&restaurantId='+this.data.restaurantId+'&parentid='+parentid+'&scheduledTime='+this.data.scheduledTime+'&foodid='+foodid+'&packingCharges='+packingCharges,
        })
      }else{
        wx.showToast({
          title: '不在点餐时间内',
          icon: 'none'
        })
      }
    }
  },
  //判断点餐时间
  time_range (beginTime, endTime) {
    var beginTime = beginTime.substring(0,5);
    var strb = beginTime.split (":");
    if (strb.length != 2) {
        return false;
    }
    var endTime = endTime.substring(0,5);
    var stre = endTime.split (":");
    if (stre.length != 2) {
        return false;
    }
    var b = new Date ();
    var e = new Date ();
    var n = new Date ();
    
    b.setHours (strb[0]);
    b.setMinutes (strb[1]);
    e.setHours (stre[0]);
    e.setMinutes (stre[1]);
    if (n.getTime () - b.getTime () >= 0 && n.getTime () - e.getTime () <= 0) {
        return true;
    } else {
        return false;
    }
},
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    //this.getCourseList();

    this.component_msg = this.selectComponent("#component_msg")
    this.component_index.loadData();
    
    this.component_msg.loadData();
    this.loadunreadNumber();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },


  onShow: function () {
 
  },
  begin(){
    wx.navigateTo({
      url: '../dishesList/dishesList?restaurantId=' + this.data.restaurantId + '&isScheduled=' + this.data.isScheduled + '&scheduledTime=' + this.data.scheduledTime + '&orderType=' + this.data.orderType,
    })
  },
  molder(e){
    var that = this;
    let index = e.currentTarget.dataset.index
    
    var couponType = 1;//餐厅

    switch (index) {
      case 1:
        wx.navigateTo({//优惠券
          url: "../coupon_canteen/coupon_canteen?couponType="+couponType+"&restaurantId="+that.data.restaurantId+"&paypage=true"+"&totalPrice=",
        })
        
        break;
     // case 2:
     case 0:
        wx.navigateTo({//订单列表
          url: '../orderList_canteen/orderList_canteen',
        })
        break;
  
    }
  },
  loadData(){
    var that = this;
  },
})
